package com.heima.admin.config;

import com.heima.admin.service.AdSensitiveService;
import com.heima.feign.admin.AdminFeign;
import com.heima.model.common.constants.wemedia.WemediaConstants;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 初始化敏感词列表
 */
@Component
@Slf4j
public class SensitiveInit implements InitializingBean {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    AdminFeign adminFeign;
    @Autowired
    AdSensitiveService adSensitiveService;

    @Override
    public void afterPropertiesSet() {
        log.info("敏感词列表初始化中");
        //获取0号库的敏感词列表
        Set<String> members = redisTemplate.opsForSet().members(WemediaConstants.SENSITIVE);

        //Redis中已经有数据
        if (members != null && members.size() > 0)
            return;
        //调用查询敏感词列表
        ResponseResult<List<String>> listResponseResult = adSensitiveService.selectAllSensitives();
        List<String> data = listResponseResult.getData();

        for (String datum : data) {
            redisTemplate.opsForSet().add(WemediaConstants.SENSITIVE,datum);
        }

        log.info("敏感词列表初始化完毕...");
    }
}
